from flask import Flask, request, jsonify

app = Flask(__name__)

# 用于存储用户数据的字典
users = {}

# 创建用户
@app.route('/users', methods=['POST'])
def create_user():
    user_data = request.get_json()
    user_id = user_data.get('id')
    if user_id in users:
        return jsonify({'message': 'User already exists'}), 400
    users[user_id] = user_data
    return jsonify(user_data), 201

# 获取所有用户
@app.route('/users', methods=['GET'])
def get_users():
    return jsonify(list(users.values()))

# 获取特定用户
@app.route('/users/<user_id>', methods=['GET'])
def get_user(user_id):
    user = users.get(user_id)
    if not user:
        return jsonify({'message': 'User not found'}), 404
    return jsonify(user)

# 更新用户
@app.route('/users/<user_id>', methods=['PUT'])
def update_user(user_id):
    user_data = request.get_json()
    if user_id not in users:
        return jsonify({'message': 'User not found'}), 404
    users[user_id].update(user_data)
    return jsonify(users[user_id])

# 删除用户
@app.route('/users/<user_id>', methods=['DELETE'])
def delete_user(user_id):
    if user_id not in users:
        return jsonify({'message': 'User not found'}), 404
    del users[user_id]
    return jsonify({'message': 'User deleted'}), 200

if __name__ == '__main__':
    app.run(debug=True)